//
//  SecurityDetailVC.m
//  PrivateSteward
//
//  Created by lanou3g on 16/4/1.
//  Copyright © 2016年 闫贺晨. All rights reserved.
//

#import "SecurityDetailVC.h"

@interface SecurityDetailVC ()
//是否处于编辑状态
@property(nonatomic,assign)BOOL isEdit;
@end

@implementation SecurityDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    if ( self.isAdd) {
        self.navigationItem.title = @"密码添加";
        [self.addBtton setTitle:@"添加" forState:UIControlStateNormal];
    }
    else
    {
        self.isEdit = NO;
        self.pwdNameTextFiled.text = self.pwdName;
        self.pwdTextFiled.text = self.pwd;
        self.pwdNameTextFiled.enabled = NO;
        self.pwdTextFiled.enabled = NO;
        self.navigationItem.title = @"密码编辑";
        [self.addBtton setTitle:@"编辑" forState:UIControlStateNormal];
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
}
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)addAction:(UIButton *)sender {
    if (self.isAdd) {
        //向数据库中添加新的数据
        AVObject *obj = [[AVObject alloc]initWithClassName:@"securityHelper"];
        [obj setObject:[[AVUser currentUser] username] forKey:@"phoneNumber"];
        [obj setObject:self.pwdNameTextFiled.text forKey:@"pwdname"];
        [obj setObject:self.pwdTextFiled forKey:@"pwd"];
        [obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            //保存成功后,处于非编辑状态
            self.isEdit = NO;
            [self.addBtton setTitle:@"编辑" forState:UIControlStateNormal];
             self.navigationItem.title = @"密码编辑";
            [self alertAction:@"成功" messageString:@"该信息已成功保存"];
        }];
    }
    else
    {
        if (self.isEdit == NO) {
            self.isEdit = YES;
            [self.addBtton setTitle:@"编辑" forState:UIControlStateNormal];
            self.pwdTextFiled.enabled = YES;
            self.pwdNameTextFiled .enabled = YES;
        }
        else
        {
           //处于编辑状态,对数据库里的东西进行修改
            AVQuery *query = [AVQuery queryWithClassName:@"securityHelper"];
            [query whereKey:@"phoneNumber" equalTo:[[AVUser currentUser] username]];
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                AVObject *obj = [AVObject objectWithoutDataWithClassName:@"securityHelper" objectId:[objects[0] objectId]];
                [obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                    if (succeeded) {
                        [self.addBtton setTitle:@"编辑" forState:UIControlStateNormal];
                        self.isEdit = NO;
                        [self alertAction:@"成功" messageString:@"已修改成功"];
                    }
                }];
            }];
        }
    }
}
-(void)alertAction:(NSString *)alertstring messageString:(NSString *)messageString
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:alertstring message:messageString preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:^{
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
